import java.util.*;
import java.io.*;

public class ReadHMMProfile {
	
	private String ProfileFileName;
	private File file=null;
	private BufferedReader reader=null;
	
	// open the file which contains multiply HMMER profiles
	// Use BufferReader to read the file line by line
	public ReadHMMProfile (String profileFile){

		try {
			ProfileFileName = profileFile;
			file = new File(ProfileFileName);
			reader = new BufferedReader(new FileReader(file));

		} catch (FileNotFoundException NoFile) {
			NoFile.printStackTrace();
		}
	}
	
	
	
	// read in certain lines which contains a complete HMMER profile
	public boolean ReadNextProfile( HMMProfile profile ){

		String ProfileBuffer = new String();
		String line = new String();
		
	
		try {
			while (line.contains("NAME") == false) {
				line = reader.readLine();
				if (line==null)
					return false;
			}
		} catch (IOException e) {
			return false;
		}
		
		profile.SetHMMProfileName(line);

		try {
			//determine where is the beginning of HMMER profile by detecting "COMPO" word
			while (line.contains("COMPO") == false) {
				line = reader.readLine();
				if (line==null)
					return false;
			}
			//add all lines after beginning of HMMER profile until meet "//" word
			do {
				ProfileBuffer = ProfileBuffer + line + "\r";
				line = reader.readLine();
				if (line==null)
					return false;
			} while (!line.contains("//"));
	} catch (IOException e) {
		//e.printStackTrace();
	}

				
		//use obtained string to created a HMMER profile instance and return this instance	
		//HMMProfile HMMprofile = new HMMProfile();
		//HMMprofile.SetHMMProfile(ProfileBuffer);
		profile.SetHMMProfile(ProfileBuffer);
		return true;
		
	
	}
	
	public boolean Reset( ){
		
		try {
			reader.close();
			file = new File(ProfileFileName);
			reader = new BufferedReader(new FileReader(file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return false;
		} 
		
		return true;
	}
	
	/*
	public static void main (String[] args){
		
		HMMProfile One= new HMMProfile();

		HMMProfile Two= new HMMProfile();

		HMMProfile Three= new HMMProfile();
		boolean SuccessfulRead=false;

		ReadHMMERProfile ReadProfile = new ReadHMMERProfile("/home/miles/workspace/FloatCloud/bin/sample1.hmm");
		
	
		
		if (!(SuccessfulRead=ReadProfile.ReadNextProfile(One)))
		{
			ReadProfile.Reset();
		}
		
		if (!(SuccessfulRead=ReadProfile.ReadNextProfile(Two)))
		{
			ReadProfile.Reset();
		}
		
		if (!(SuccessfulRead=ReadProfile.ReadNextProfile(Three)))
		{
			ReadProfile.Reset();
		}
		

		
	}
	*/
	
}
